package controller;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import model.AppModel;
import view.AppView;
import view.ComposeMailView;

public class NavigationController {

	// Application model and view
	private AppModel model;
	private AppView view;
	
	/**
	 * Constructor
	 * @param model
	 * @param view
	 */
	public NavigationController(AppModel model, AppView view) {
		this.model = model;
		this.view = view;
		assignHandlersToEvents();
	}
	
	/**
	 * All possible actions for this controller
	 * 
	 * IMPORTANT: Always create a new method for every action,
	 * don't create any action logic directly under this function.
	 * This function is supposed to be the event listener, and actions
	 * should be separate functions.
	 */
	private void assignHandlersToEvents(){
	
		// Compose button click 
		view.getNavigation().getComposeBtn().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionComposeMail(e);
			}
		});
		
		// Right click on any of the folders
		view.getNavigation().getFoldersList().addMouseListener(new MouseAdapter(){
			@Override
			public void mousePressed( MouseEvent e ){
				if ( SwingUtilities.isRightMouseButton(e)){
					actionShowRightClickMenu(e);
				}
				
			}
		});

	}
	
	// Shows the right click menu on the folder name
	protected void actionShowRightClickMenu(MouseEvent e) {
		
		// Selecting the currently right clicked folder
		JList foldersList = view.getNavigation().getFoldersList();
		Point p = e.getPoint();
		int index = foldersList.locationToIndex( p );
		ListSelectionModel selectionModel = foldersList.getSelectionModel();
		selectionModel.setSelectionInterval(index, index);

		// Creating the popup menu with options
		JPopupMenu popup = new JPopupMenu("Folder options");
		JMenuItem moveItem = new JMenuItem(new AbstractAction("Delete") {
		    public void actionPerformed(ActionEvent e) {
		        actionDeleteFolder();
		    }
		});
		JMenuItem deleteItem = new JMenuItem(new AbstractAction("Rename") {
		    public void actionPerformed(ActionEvent e) {
		        actionRenameFolder();
		    }
		});
		
		popup.add(moveItem);
		popup.add(deleteItem);
		popup.show(e.getComponent(), e.getX(), e.getY());
	}

	/**
	 * Rename folder action
	 */
	protected void actionRenameFolder() {
		System.out.println("Rename folder action");
		Object[] temp = {"Folder 1", "Folder 2"};
		JOptionPane popup = new JOptionPane();
		JTextField newName = new JTextField();
		JOptionPane.showMessageDialog(
		  null,
		  newName,
		  "Enter new folder name",
		  JOptionPane.PLAIN_MESSAGE);
		System.out.println("New name: " + newName.getText());
	}

	/**
	 * Delete folder action
	 */
	protected void actionDeleteFolder() {
		int option = JOptionPane.showConfirmDialog (null, "Are you sure you want to delete the selected folder?");
		if (option == JOptionPane.YES_OPTION ) 
		{
			System.out.println("Delete the folder");
			JList foldersList = view.getNavigation().getFoldersList();
			
			int selectedIndex = foldersList.getSelectedIndex(); 
			DefaultListModel folders = view.getNavigation().getFolders();
						
			folders.remove(selectedIndex);			
		}
	}

	/**
	 * Compose button click action
	 * @param e
	 */
	private void actionComposeMail(ActionEvent e){
		new ComposeMailController();
	}
	
	

}
